//并发请求限制

export const concurrency = async (requestList:any[], limit:number,success,error) => {
	const result = [];
	const len = requestList.length;
	const next = async () => {
		while (requestList.length) {
			const index = len - requestList.length
			const request = requestList.pop();
			try {
				result[index] = await request()
				success(result[index])
			} catch (e) {
				error(e)
				result[index] = e
			}
		}
	};

	await Promise.all(new Array(limit).fill(null).map(() => next()));

	return result;
};
